#include "Dato.h"

void Dato::reverse(char s[]){
     int i, j;
     char c;
     for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
         c = s[i];
         s[i] = s[j];
         s[j] = c;
     }
}

void Dato::intToString(int n, char s[]){
     int i, sign;
     if ((sign = n) < 0)  
         n = -n;          
     i = 0;
     do{       
         s[i++] = n % 10 + '0';   
     } while ((n /= 10) > 0);     
     if (sign < 0)
         s[i++] = '-';
     s[i] = '\0';
     this->reverse(s);
} 
              
int Dato::stringToInt(char s[]){
     return atoi(s);
}
              
void Dato::floatToString(float n, char s[]){
     this->intToString((int)n,s);
     int aux = (int) n;
     float decimal = (float) (n - aux);
     if(decimal<0)
          decimal = -decimal;
     float auxDec = decimal;
     if(auxDec<=0)
          return;
     strcat(s,".");
     while(auxDec>0){
          char cad[2]="x";
          auxDec *= 10;
          cad[0] = '0' + (int) auxDec;
          strcat(s,cad);
          aux = (int) auxDec;            
          auxDec = (float) (auxDec - aux);                                       
     }
}
              
float Dato::stringToFloat(char s[]){
      return atof(s);
}
              
char Dato::boolToChar(bool b){
     if(b)
          return 'T';
     return 'F';
}
              
bool Dato::charToBool(char c){
     if(c=='T')
          return true;
     return false;
}
